package com.demo.hystrix.controller;

import com.demo.common.entity.User;
import com.demo.common.feign.UserFeign;
import com.demo.common.util.R;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

@RestController
@AllArgsConstructor
@RequestMapping("/hystirx")
@Slf4j
public class HystrixController {

    private final UserFeign userFeign;

    @PostMapping("/test")
    @HystrixCommand(fallbackMethod = "getUserFallBack")
    public R getUser(@RequestBody User user) {
        log.info("--------------------------");
        return new R<>(userFeign.getUser(user));
    }

    public R getUserFallBack(User user) {
        log.info("===========================");
        user.setName("请求失败");
        return new R<>(user);
    }
}
